package com.taotao.test2.easy;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.support.ExcelTypeEnum;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
 * @program: test2
 * @ClassName ExcelUtils
 * @description:
 * @author: wangjin
 * @create: 2023-06-08 17:05
 * @Version 1.0
 **/
public class ExcelUtils {
    public static String exportVoteExcel(String fileName, String sheetName, Class clazz,
                                         List dataList, String title, String describe, Date voteDate) throws IOException, IOException {
        File result = File.createTempFile(fileName, ".xlsx", new File("D://"));
        EasyExcel.write(result)
                .head(clazz)
                .excelType(ExcelTypeEnum.XLSX)
                .registerWriteHandler(new VoteTitleHandler(title, describe, voteDate))
                .sheet(sheetName)
                .doWrite(dataList);
        return null;
    }


    private void modifyFileds(Class clazz, Consumer<Map> consumer) throws Exception {
        // 获取实体类字段
        Field field = clazz.getDeclaredField("username");
        // 获取实体类字段的注解类
        ExcelProperty annotation = field.getAnnotation(ExcelProperty.class);
        // 将注解类生成一个代理对象
        InvocationHandler invocationHandler = Proxy.getInvocationHandler(annotation);
        Field annotationValues = invocationHandler.getClass().getDeclaredField("memberValues");
        annotationValues.setAccessible(true);
        Map map = (Map) annotationValues.get(invocationHandler);
        consumer.accept(map);
    }


}
